package com.sls.mall.member.service.impl;

import com.sls.mall.member.dao.MemberLevelDao;
import com.sls.mall.member.entity.MemberLevelEntity;
import com.sls.mall.member.exception.PhoneExsitException;
import com.sls.mall.member.exception.UsernameExistException;
import com.sls.mall.member.vo.MemberLoginVo;
import com.sls.mall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sls.common.utils.PageUtils;
import com.sls.common.utils.Query;

import com.sls.mall.member.dao.MemberDao;
import com.sls.mall.member.entity.MemberEntity;
import com.sls.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo memberRegistVo) {
        MemberDao memberDao = this.baseMapper;

        MemberEntity memberEntity = new MemberEntity();

        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        //检测手机号及用户名是否唯一
        // 不报异常即为通过
        checkPhoneUnique(memberRegistVo.getPhone());
        checkUsernameUnique(memberRegistVo.getUserName());

        memberEntity.setMobile(memberRegistVo.getPhone());
        memberEntity.setUsername(memberRegistVo.getUserName());
        memberEntity.setNickname(memberRegistVo.getUserName());
        //密码 md5 盐值加密 BCryptPasswordEncoder 好处是 盐值不需要存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //盐值加密
        String encode = passwordEncoder.encode(memberRegistVo.getPassword());
        //解密 参数：1 明文密码  2 数据库存储的加密密码
//        passwordEncoder.matches(memberRegistVo.getPassword(),encode);

        memberEntity.setPassword(encode);

        memberDao.insert(memberEntity);
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginacct())
                .or().eq("mobile", vo.getLoginacct()));
        if(memberEntity != null){
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            if(encoder.matches(vo.getPassword(), memberEntity.getPassword())){
                return memberEntity;
            }
        }
        return null;
    }

    public void checkUsernameUnique(String userName) throws UsernameExistException {

        MemberDao memberDao = this.baseMapper;

        Integer username = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (username > 0) {
            throw new UsernameExistException();
        }


    }

    public void checkPhoneUnique(String phone) throws PhoneExsitException {

        MemberDao memberDao = this.baseMapper;

        Integer mobile = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));

        if (mobile > 0) {
            throw new UsernameExistException();
        }


    }
}